<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态添加表格内容</title>
    <style>
        table {
            width: 500px;
            height: 20px;
            border: 1px solid black;
            border-collapse: collapse;
            border-spacing: 0;
        }
        
        th,
        td {
            text-align: center;
            border: 1px solid black;
        }
        
        th {
            background: purple;
        }
        
        td {
            background: pink;
        }
    </style>

</head>

<body>
    <div>
        <label>请输入姓名：</label><input type="text" id="username"><br>
        <label>请输入邮箱：</label><input type="text" id="email"><br>
        <button id="add">添加</button>
        <table>
            <thead>
                <tr>
                    <th>姓名</th>
                    <th>邮箱</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>小黑</td>
                    <td>xiaohei@126.com</td>
                </tr>
            </tbody>
        </table>
    </div>

    <script>
        var add = document.getElementById('add')
        add.onclick = function() {
            var username = document.getElementById('username').value
            var email = document.getElementById('email').value
            if (username.value != '' && email.value != '') {
                var newtd = document.createElement('td')
                var newtd1 = document.createElement('td')
                newtd.appendChild(document.createTextNode(username))
                newtd1.appendChild(document.createTextNode(email))
                var newtr = document.createElement('tr')
                newtr.appendChild(newtd)
                newtr.appendChild(newtd1)
                document.querySelector('tbody').appendChild(newtr)
            }
        }
    </script>
</body>

</html>